package com.scorekeeper.utils;

import java.io.IOException;
import java.util.Map;

import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.scorekeeper.objects.Division;
import com.scorekeeper.objects.Extension;
import com.scorekeeper.objects.Extension.CastType;
import com.scorekeeper.utils.Loader;

public class HTTPLoad {
	private final static String PROXY_HOST = "internet2.can.eds.com";
	private final static int PROXY_PORT = 8080;
	private final static String PROXY_UID = "siegleda";
	private final static String PROXY_PWD = "H@xorz07";
	
	public static boolean useProxy = true;
	
	public static Document getDocument(String url, String... keyvals) throws IOException {
		try {
                HttpConnection jsConn = HttpConnection.connect(url);
                if (useProxy)
                	jsConn.proxy(PROXY_HOST, PROXY_PORT, PROXY_UID, PROXY_PWD);
                
                jsConn.userAgent("Mozilla");
                jsConn.data(keyvals);
                return jsConn.get();
        } catch (IOException e) {
            //Auto-generated catch block
        	Logger.LogError(e);
        	throw e;
        } 
    }
	
	public static Document loadSchedule(String teamAcro, String season, String venue) {
		Document doc = null;
		try {
			doc = getDocument("http://www.nhl.com/ice/schedulebyseason.htm", "season", season, "gameType" , "2", "team", teamAcro, "venue", venue);
		} catch (IOException e) {
			return null;
		}
		return doc;
	}
	
	public static Document loadStandings() {
		try {
			return getDocument("http://www.nhl.com/ice/standings.htm", "type", "lea");
		} catch (IOException e) {
			return null;
		}
	}
	
	public static Map<Long, Division> buildDivisionData(Document doc) {
		Division division = null;
		
		//attempt to load the divisions first
		Map<Long, Division> divisions = null;
		divisions = (new Loader()).loadDivisions();
		
		if (divisions == null) { 
			Elements divs = doc.body().select("#navTeams").select("div");
			divs.remove(0);
			
			for (Element div : divs) {
				division = new Division();
				division.name = div.text();
				if (division.name.contains("west")) {
					division.acro = division.name.substring(0, 1).toUpperCase() + "W";
				} else if (division.name.contains("east")) {
					division.acro = division.name.substring(0, 1).toUpperCase() + "E";
				} else {
					division.acro = division.name.substring(0, 3).toUpperCase();
				}
				division.save();
			}
			
			divisions = (new Loader()).loadDivisions();
		}
		
		return divisions;
	}
	
	private static Extension getExtension(String key, Map<String, Extension> extensions) {
		if (extensions != null) {
			if (extensions.get(key) != null) {
				return extensions.get(key);
			}
		}
		
		return new Extension();
	}
	
	public static void buildTeamExtensionData(Elements data, int id, Map<String, Extension> extensions, int itemIdx) {
		Extension ext = null; 
		
		if (data.get(5).tagName() == "span") {
			ext = getExtension(Extension.ExtensionConstants.CLINCH, extensions);
			ext.teamId = id;
			ext.name = Extension.ExtensionConstants.CLINCH;
			ext.value = data.get(5).childNode(0).toString().substring(0, 1);
			ext.setExtType(CastType.STR.toInt());
			ext.save();
		}
		
		ext = getExtension(Extension.ExtensionConstants.ROW, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.ROW;
		ext.value = data.get(++itemIdx).childNode(0).toString();
		ext.setExtType(CastType.INT.toInt());
		ext.save();
		
		ext = getExtension(Extension.ExtensionConstants.GF, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.GF;
		ext.value = data.get(++itemIdx).childNode(0).toString();
		ext.setExtType(CastType.INT.toInt());
		ext.save();
		
		ext = getExtension(Extension.ExtensionConstants.GA, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.GA;
		ext.value = data.get(++itemIdx).childNode(0).toString();
		ext.setExtType(CastType.INT.toInt());
		ext.save();
		
		ext = getExtension(Extension.ExtensionConstants.HOME, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.HOME;
		ext.value = data.get(itemIdx += 3).childNode(0).toString().replace("\n", "");
		ext.setExtType(CastType.STR.toInt());
		ext.save();
		
		ext = getExtension(Extension.ExtensionConstants.AWAY, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.AWAY;
		ext.value = data.get(itemIdx += 2).childNode(0).toString().replace("\n", "");
		ext.setExtType(CastType.STR.toInt());
		ext.save();
		
		ext = getExtension(Extension.ExtensionConstants.SO, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.SO;
		ext.value = data.get(++itemIdx).childNode(0).toString();
		ext.setExtType(CastType.STR.toInt());
		ext.save();
		
		ext = getExtension(Extension.ExtensionConstants.LASTTEN, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.LASTTEN;
		ext.value = data.get(++itemIdx).childNode(0).toString();
		ext.setExtType(CastType.STR.toInt());
		ext.save();
		
		ext = getExtension(Extension.ExtensionConstants.STREAK, extensions);
		ext.teamId = id;
		ext.name = Extension.ExtensionConstants.STREAK;
		ext.value = data.get(++itemIdx).childNode(0).toString();
		ext.setExtType(CastType.STR.toInt());
		ext.save();
		
	}
}